#!/bin/bash

###run this script to process a list, the list contains package info which should 
###be adapted by script Change_Spec_License.sh
###parameters:
###		package_list: package info list file format as follows
###			      package_name:old_license_info:new_license_info
###		submitter   : author info used to generate changelog
###		mail        : e mail address used to genertae changelog
###output    :  NULL
###author    : Wang Ge
###date      : 2024-06-12

Usage="Usage : ./process_list.sh package_list submitter mail"
Exam="Exapmple: ./process_list.sh list 'Ge Wang' '<wang__ge@126.com>'"

LIST_NAME=$1
AUTHOR_NAME=$2
MAIL_ADDR=$3

if [ $# -ne 3 ]; then
  echo $Usage
  echo $Exam
  exit 1
fi

if [ ! -f "./Change_Spec_License.sh" ];then
  echo "Error: no Change_Spec_License.sh found"
  exit 1
fi

if [ ! -f "$LIST_NAME" ];then
  echo "Error: no valid package_list file found"
  exit 1
fi

LINE_NUM=$(awk 'END{print NR}' $LIST_NAME)

while [ $LINE_NUM -ne 0 ]
do 
  PACKAGE_NAME=`sed -n "$LINE_NUM, 1p" $LIST_NAME | awk -F: '{print $1}'`
  OLD_LICENSE=`sed -n "$LINE_NUM, 1p" $LIST_NAME | awk -F: '{print $2}'`
  NEW_LICENSE=`sed -n "$LINE_NUM, 1p" $LIST_NAME | awk -F: '{print $3}'`
  ./Change_Spec_License.sh $PACKAGE_NAME "$OLD_LICENSE" "$NEW_LICENSE" "$AUTHOR_NAME" "$MAIL_ADDR"
  #echo $PACKAGE_NAME $OLD_LICENSE $NEW_LICENSE
  #echo $((LINE_NUM))
  LINE_NUM=$((LINE_NUM - 1))
done
#echo $LINE_NUM
#echo $LIST_NAME
